package xworker.deploy;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Deployer {
	public static void checkFile(String filePath){
		File file = new File(filePath);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	public static void main(String args[]){
		try{
			checkFile("./xworker/classes/");
			checkFile("./xworker/lib/");
			checkFile("./xworker/databases/");
			checkFile("./xworker/xworker_remote/things/");
			checkFile("./xworker/projects/_local/things/_local/xworker/config/");
			
			
			List<File> files = new ArrayList<File>();			
			files.add(new File("./xworker/classes/"));
			
			File libs = new File("./xworker/lib/");
			for(File child : libs.listFiles()){
				if(child.isFile()){
					files.add(child);
				}
			}
			
			URL[] urls = new URL[files.size()];
			for(int i=0; i<files.size(); i++){
				urls[i] = files.get(i).toURI().toURL();
			}
			
			RemoteClassLoader loader = new RemoteClassLoader(urls);	
			
			Class deployer = loader.findClass("xworker.app.xworker.XWorkerDeployer");
			Method run = deployer.getMethod("main", new Class[]{Object.class});
			run.invoke(null, new Object[]{loader});
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
